package com.aoxin.service.impl.opm;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.aoxin.mapper.SlfClassroomEmployMapper;
import com.aoxin.mapper.opm.SlfSchoolMapper;
import com.aoxin.model.opm.SlfSchool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.opm.SlfClassroomMapper;
import com.aoxin.mapper.opm.SlfClassroomSyllabusDateMapper;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfClassroom;
import com.aoxin.service.inter.opm.ClassroomService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("classroomService")
public class ClassroomServiceImpl implements ClassroomService {

	@Autowired
	private SlfClassroomMapper slfClassroomMapper;

	@Autowired
	private SlfClassroomSyllabusDateMapper slfClassroomSyllabusDateMapper;

	@Autowired
	private SlfSchoolMapper slfSchoolMapper;
	
	@Autowired
	private SlfClassroomEmployMapper slfClassroomEmployMapper;
	

	@Override
	@Transactional
	public Integer insertSlfClassroom(SlfClassroom classroom) {
		Assert.notNull(classroom, String.format(ErrorUtil.PARAMETER_NULL, "classroom"));
		classroom.setCreateDate(new Date());
		if (null == classroom.getSequence()) {
			classroom.setSequence(this.slfClassroomMapper.getNextSequence(classroom.getSchoolId()).intValue());
		}
		if (this.slfClassroomMapper.checkName(classroom.getName(), null) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, classroom.getName()));
		}
		this.slfClassroomMapper.insertSelective(classroom);
		return classroom.getClassroomId();
	}

	@Override
	@Transactional
	public void deleteSlfClassroom(Integer classroomId) {
		Assert.notNull(classroomId, String.format(ErrorUtil.PARAMETER_NULL, "classroomId"));
		//判断是否存在开班的这个教室
		Integer a = slfClassroomEmployMapper.IsClassroomEmployOther(classroomId);
		if (a > 0) {
			throw new IllegalArgumentException(String.format(ErrorUtil.CLASSROOMCZ));
		}
		//删除占用表
		
		//删除绑定包表
		
		this.slfClassroomMapper.deleteByPrimaryKey(classroomId);
		
	}

	@Override
	@Transactional
	public void deleteSlfClassroomByIds(List<Integer> classroomIds) {
		Assert.notEmpty(classroomIds, String.format(ErrorUtil.PARAMETER_NULL, "classroomIds"));
		for (Integer integer : classroomIds) {
			Integer a = slfClassroomEmployMapper.IsClassroomEmployOther(integer);
			if (a > 0) {
				throw new IllegalArgumentException(String.format(ErrorUtil.CLASSROOMCZ));
			}
	//		this.slfClassroomSyllabusDateMapper.deleteByClassroomId(integer);
		}
		this.slfClassroomMapper.deleteByPrimaryKeys(classroomIds);
	}

	@Override
	@Transactional
	public void updateSlfClassroom(SlfClassroom classroom) {
		Assert.notNull(classroom, String.format(ErrorUtil.PARAMETER_NULL, "classroom"));
		if (this.slfClassroomMapper.checkName(classroom.getName(), classroom.getClassroomId()) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, classroom.getName()));
		}
		
		if(classroom.getState()==0){
			//禁用
			if("true".equals(slfClassroomMapper.queryClassRoomBindingPackage(classroom.getClassroomId()))){
				throw new IllegalArgumentException("已绑定教室课程包不允许禁用");
			}
		}
		this.slfClassroomMapper.updateByPrimaryKeySelective(classroom);
	}

	@Override
	@Transactional
	public void updateSlfClassroomStates(List<Integer> classroomIds, Integer state) {
		Assert.notEmpty(classroomIds, String.format(ErrorUtil.PARAMETER_NULL, "classroomIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
		
		if(state==0){
			//禁用
			for (int i = 0,b=classroomIds.size(); i <b; i++) {
				if("true".equals(slfClassroomMapper.queryClassRoomBindingPackage(classroomIds.get(i)))){
					throw new IllegalArgumentException("已绑定教室课程包不允许禁用");
				}
			}
		}
		
		
		this.slfClassroomMapper.updateStates(classroomIds, state);
	}

	@Override
	public SlfClassroom getSlfClassroom(Integer classroomId) {
//		Assert.notNull(classroomId, String.format(ErrorUtil.PARAMETER_NULL, "classroomId"));
		return this.slfClassroomMapper.selectByPrimaryKey(classroomId);
	}

	@Override
	public List<SlfClassroom> getSlfClassroomList(Map<String, Object> params) {
		if(params.get("state")==null || "".equals(params.get("state"))){
			params.put("state", 1);
		}
		List<SlfClassroom> slfClassroomList = this.slfClassroomMapper.getList(params);
		for (SlfClassroom slfClassroom : slfClassroomList) {
			SlfSchool school = this.slfSchoolMapper.selectByPrimaryKey(slfClassroom.getSchoolId());
			if (null != school) {
				slfClassroom.setSchoolName(school.getName());
			}
		}
		return slfClassroomList;
	}

	@Override
	public Long getNextSequence(Integer schoolId) {
		return this.slfClassroomMapper.getNextSequence(schoolId);
	}

	@Override
	public List<SelectDto> getListBySchoolId() {
		return this.slfClassroomMapper.getListBySchoolId(ThreadLocalUtil.getEmployee().getSchoolId());
	}

	@Override
	public void insertRoom(SlfClassroom classroom) {
		this.slfClassroomMapper.insertSelective(classroom);
	}
}
